

import os
# uncompyle6 version 2.11.5
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.9 (default, Sep 17 2016, 20:26:04)
# [GCC 4.9.2]
# Embedded file name: D:\project\inleader_frist_client\print_client\main.py
# Compiled at: 2014-01-11 03:42:25
import sys

from PyQt5 import QtWebKit
from PyQt5.QtCore import QObject, QSize, QRect, pyqtSignal, QUrl, Qt
# from PyQt5.
from PyQt5.QtNetwork import QNetworkDiskCache
from PyQt5.QtWebKitWidgets import QWebPage
from PyQt5.QtWidgets import QDialog, QApplication

from core.UI_thread import UIThread
from core.clientCode_thread import ClientCodeThread
from core.rpc_thread import RpcThread
# from core.wifi_api import WifiApi, WifiSignal
from setting import setting, Vfs
from ui import ui_main


class MainSignal(QObject):
    returnMacSignal = pyqtSignal(str)
    returnServerSignal = pyqtSignal(str)
    refreshPrintCodeSignal = pyqtSignal(str)
    updatePrintProgressSignal = pyqtSignal(int)
    updatePrintPotoSignal = pyqtSignal(str)
    downloadSuccessSignal = pyqtSignal()
    updatePrintPotoFailureSignal = pyqtSignal()
    getVerifyCodeFailureSignal = pyqtSignal()
    checkNetWorkFailureSignal = pyqtSignal()
    getSpecialCode = pyqtSignal()
    downloadFailuerSignal = pyqtSignal()
    refreshUISignal = pyqtSignal()


class DebugWebPage(QWebPage):

    def javaScriptConsoleMessage(self, msg, lineNumber, sourceID):
        setting.logger.debug('javascript (%s:%d): %s' % (sourceID, lineNumber, msg))


class MainDialog(QDialog, ui_main.Ui_MainDialog):

    def __init__(self):
        QDialog.__init__(self)
        self.setupUi(self)
        http_cache = QNetworkDiskCache()
        http_cache.setCacheDirectory(Vfs.getWebkitCacheDir())
        self.webView.page().networkAccessManager().setCache(http_cache)
        self.webView.setPage(DebugWebPage(self.webView))
        self.signal = MainSignal()
        # self.wifi_api = WifiApi()
        # self.wifi_signal = WifiSignal()
        self.webView.page().mainFrame().javaScriptWindowObjectCleared.connect(self.exposedJavaScript)
        self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
        self.webView.loadFinished.connect(self.loadFinished)
        self.thread = RpcThread(self)
        self.clientThread = ClientCodeThread(self)
        self.uiThread = UIThread(self)
        self.clientThread.start()
        self.uiThread.start()

    def loadFinished(self):
        if self.thread.ident is None:
            self.thread.start()
            self.onReturnServer(setting.config.get('api', 'server'))
            self.onReturnMac(setting.getMac())
        return

    def loadUrl(self, qurl):
        print (qurl)

        self.webView.load(qurl)

    def exposedJavaScript(self):
        self.webView.page().mainFrame().addToJavaScriptWindowObject('MainSignal', self.signal)
        # self.webView.page().mainFrame().addToJavaScriptWindowObject('Wifi', self.wifi_api)
        # self.webView.page().mainFrame().addToJavaScriptWindowObject('WifiSignal', self.wifi_api.signal)

    def onReturnMac(self, mac):
        self.signal.returnMacSignal.emit(mac)

    def onReturnServer(self, server):
        self.signal.returnServerSignal.emit(server)

    def onRefreshPrintCode(self, code):
        self.signal.refreshPrintCodeSignal.emit(code)
        self.onReturnServer(setting.config.get('api', 'server'))
        self.onReturnMac(setting.getMac())

    def onUpdatePrintProgress(self, progress):
        if progress != 0:
            self.signal.updatePrintProgressSignal.emit(progress)

    def onUpdatePrintPotoFailure(self):
        self.signal.updatePrintPotoFailureSignal.emit()

    def onUpdatePrintPoto(self, printImage):
        if printImage != '':
            path = QUrl.fromUserInput(printImage)
            self.signal.updatePrintPotoSignal.emit(path.toString())

    def onDownloadSuccess(self):
        self.signal.downloadSuccessSignal.emit()

    def onGetVerifyCodeFailure(self):
        self.signal.getVerifyCodeFailureSignal.emit()

    def onCheckNetWorkFailureSignal(self):
        self.signal.checkNetWorkFailureSignal.emit()

    def onGetSpecialCode(self):
        self.signal.getSpecialCode.emit()

    def onDownloadFailuer(self):
        self.signal.downloadFailuerSignal.emit()

    def onRefreshUI(self):
        self.signal.refreshUISignal.emit()

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:
            self.close()

    def mousePressEvent(self, event):
        if 0 <= event.x() <= 4 and 0 <= event.y() <= 4:
            self.close()

    def closeEvent(self, event):
        self.thread._Thread__stop()
        self.clientThread._Thread__stop()
        self.uiThread._Thread__stop()


def main():
    app = QApplication(sys.argv)
    mainDlg = MainDialog()
    desktop = QApplication.desktop()
    mainDlg.resize(QSize(desktop.width(), desktop.height()))
    if setting.config is not None and setting.config.has_option('api', 'html_size'):
        html_size = setting.config.get('api', 'html_size')
        if '19' == str(html_size):
            mainDlg.webView.setGeometry(QRect(0, 0, 1440, 900))
        elif '15' == str(html_size):
            mainDlg.webView.setGeometry(QRect(0, 0, 1366, 768))
        elif '12' == str(html_size):
            mainDlg.webView.setGeometry(QRect(0, 0, 1024, 600))
        else:
            mainDlg.webView.setGeometry(QRect(0, 0, 1920, 1080))
    if setting.config is not None and setting.config.has_option('api', 'http_url'):
        http_url = setting.config.get('api', 'http_url')
        url = QUrl(http_url)
    else:
        index = os.path.join(Vfs.getHtmlDir(), 'index.html')
        url = QUrl.fromUserInput(index)
    mainDlg.loadUrl(url)
    mainDlg.showFullScreen()
    app.exec_()
    return


main()